#include <iostream>
#include <algorithm>

#include "array.h"

Array::Array()
{
    size = DEFAULT_SIZE;
    buffer = new int[size];
    lenght = 0;
}

Array::Array(size_t size) : size(size), lenght(size)
{
    buffer = new int[size];
 //   std::cout << "In Default!\n";
}

Array::Array(const Array& array2) : size(array2.size), lenght(array2.lenght), buffer(new int[size])
{
    for (int i = 0; i != size; i++)
        buffer[i] = array2.buffer[i];
   // std::cout << "In copy!\n";
}

Array& Array::operator = (const Array& array2)
{
    if (&array2 == this)
        return *this;
    
    lenght = array2.lenght;			
    size = array2.size;
    buffer = new int[size];
    
    for (int i = 0; i != size; i++)
        buffer[i] = array2.buffer[i];
    
    return *this;
}

void Array::swap(Array& array2)
{
    std::swap(size, array2.size);
    std::swap(lenght, array2.lenght);
    std::swap(buffer, array2.buffer);
}

Array::~Array()
{
    delete[] buffer;
}

int Array::get(const size_t index) const
{
    if (index < lenght)
        return buffer[index];
}

void Array::set(const size_t index, const int value)
{
    if (index < lenght)
        buffer[index] = value;
}

void Array::push_back(const int value)
{
    if (lenght == size)
    {
        std::cout << "size increased from " << size;
        size = size*2;
        std::cout << " to " << size << std::endl;
        
        int * new_buffer = new int[size];
        
        std::copy(buffer, buffer+lenght, new_buffer);
        delete[] buffer;
        buffer = new_buffer; 
    }
    buffer[lenght] = value;
    
    std::cout << "lenght increased from " << lenght;
    ++lenght;
    std::cout << " to " << lenght << std::endl;
}










